package com.alibaba.dubbo.demo.provider.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.alibaba.dubbo.demo.SysUserRoleInterface;
import com.alibaba.dubbo.demo.bean.PageBean;
import com.alibaba.dubbo.demo.bean.SysUserRole;
import com.alibaba.dubbo.demo.provider.utils.CommUtils;

public class SysUserRoleDaoImpl extends BaseDAO implements SysUserRoleInterface {

	@Override
	public PageBean queryAllSysUserRole(PageBean pageBean, int userid, String usercode, String username, int roleid,
			String rolecode, String rolename) {
		// TODO Auto-generated method stub
		String msql = "from SysUserRole where 1=1 ";
		if(userid>0)
		{
			msql += " and userid="+userid;
		}
		else
		{
			if(StringUtils.isNotEmpty(usercode))
				msql += " and usercode='"+usercode+"'";
			if(StringUtils.isNotEmpty(username))
				msql += " and username like '%"+username+"%'";
		}
		if(roleid>0)
		{
			msql += " and roleid="+roleid;
		}
		else
		{
			if(StringUtils.isNotEmpty(rolecode))
				msql += " and rolecode='"+rolecode+"'";
			if(StringUtils.isNotEmpty(rolename))
				msql += " and rolename like '%"+rolename+"%'";
		}
		String countSql = "select count(*) " + msql;
		return getPageBean(msql, countSql, pageBean);
	}

	@Override
	public List<SysUserRole> queryAllSysUserRoleList(int userid, String usercode, String username, int roleid,
			String rolecode, String rolename) {
		// TODO Auto-generated method stub
		String msql = "from SysUserRole where 1=1 ";
		if(userid>0)
		{
			msql += " and userid="+userid;
		}
		else
		{
			if(StringUtils.isNotEmpty(usercode))
				msql += " and usercode='"+usercode+"'";
			if(StringUtils.isNotEmpty(username))
				msql += " and username like '%"+username+"%'";
		}
		if(roleid>0)
		{
			msql += " and roleid="+roleid;
		}
		else
		{
			if(StringUtils.isNotEmpty(rolecode))
				msql += " and rolecode='"+rolecode+"'";
			if(StringUtils.isNotEmpty(rolename))
				msql += " and rolename like '%"+rolename+"%'";
		}
		return queryBean(msql);	
	}

	@Override
	public SysUserRole queryOneSysUserRole(int id) {
		// TODO Auto-generated method stub
		String msql = "from SysUserRole where mainid="+id;
		return (SysUserRole) queryBean(msql).get(0);
	}
	
	public SysUserRole queryOneSysUserRole(int userid, int roleid) {
		// TODO Auto-generated method stub
		List<SysUserRole> rolelist=  queryAllSysUserRoleList(userid,"","",roleid,"","");
		if(rolelist==null)
			return null;
		if(rolelist.size()>0)
			return rolelist.get(0);
		else
			return null;
	}

	@Override
	public boolean saveSysUserRole(SysUserRole sysRoleFunc) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().save(sysRoleFunc);
			return true;
		} catch (Exception e) {
			logger.error("新增通知异常！", e);
			return false;
		}
	}
	
	

	@Override
	public boolean updateSysUserRole(SysUserRole sysRoleFunc) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().update(sysRoleFunc);
			return true;
		} catch (Exception e) {
			logger.error("更新通知异常！", e);
			return false;
		}
	}

	@Override
	public boolean deleteSysUserRole(int id) {
		// TODO Auto-generated method stub
		SysUserRole sysRoleFunc = queryOneSysUserRole(id);
		if(sysRoleFunc==null)
			return false;
		try {
			 getHibernateTemplate().delete(sysRoleFunc);
			 return true;
		} catch (Exception e) {
			logger.error("删除通知异常！", e);
			return false;
		}
	}

	@Override
	public boolean updateSysUserRole(int userid, String rolelist) {
		// TODO Auto-generated method stub
		String sSql = "";
		List<Integer> ids = new ArrayList<Integer>();
		String[] roles = rolelist.split(";");
		System.out.print("rolelist"+rolelist);
		for (int i = 0; i < roles.length; i++) {
			System.out.print("roles[i]:"+roles[i]);
			if(CommUtils.isNumeric(roles[i]))
				ids.add(Integer.valueOf(roles[i]));
		}
		if (ids.size() < 1) 
		{
			sSql = "delete from sys_userrole where userid=" + Integer.toString(userid);
			return excuteBySql(sSql)>=0;
		}
		else
		{
			String swhereroles = "";
			for (int i = 0; i < ids.size(); i++)
			{
				if(StringUtils.isEmpty(swhereroles))
					swhereroles = ids.get(i).toString();
				else
					swhereroles += "," + ids.get(i).toString();
			}
			sSql = "delete from sys_userrole where userid=" + Integer.toString(userid) + " and roleid not in ("+swhereroles+")";
			excuteBySql(sSql);	
			System.out.print("sSql:"+sSql);
		}
		SysUserRole sysRoleFunc = null;
		for(int i=0;i<ids.size();i++)
		{
			sysRoleFunc = queryOneSysUserRole(userid,ids.get(i));
			if(sysRoleFunc == null)
			{
				sysRoleFunc = new SysUserRole();
				sysRoleFunc.setUserid(userid);
				sysRoleFunc.setRoleid(ids.get(i));
				if(!saveSysUserRole(sysRoleFunc))
					return false;
			}
		}		
		return true;
	}
}
